using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Universe.WinForms
{
    public class SearchTextBox : System.Windows.Forms.TextBox
    {
        private PictureBox pictureBox1;
        public event EventHandler Search;

        public SearchTextBox()
        {
            this.InitializeComponent();
            this.Controls.Add(pictureBox1);
            pictureBox1.Cursor = Cursors.Hand;
            pictureBox1.Click += new EventHandler(pictureBox1_Click);
        }
        
        protected void OnSearch(EventArgs e)
        {
            if (Search != null)
                Search(this, EventArgs.Empty);
        }

        void pictureBox1_Click(object sender, EventArgs e)
        {
            OnSearch(EventArgs.Empty);
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter)
            {
                OnSearch(EventArgs.Empty);
                return true;
            }

            return base.ProcessCmdKey(ref msg, keyData);
        }

        protected override void OnHandleCreated(EventArgs e)
        {
            base.OnHandleCreated(e);
            Message m = new Message();
            m.HWnd = this.Handle;
            m.Msg = 0x00D3; //EM_SETMARGINS           
            m.WParam = (IntPtr)0x0002; //EC_RIGHTMARGIN      
            int margin = pictureBox1.Image.Width;
            m.LParam = (IntPtr)((margin + 1) << 16);
            WndProc(ref m);

            // GWL_STYLE + WS_CLIPCHILDREN     
            SetWindowLongA(this.Handle, -16, GetWindowLongA(this.Handle, -16) | 0x02000000);
        }

        [DllImport("user32.dll")]
        static extern int SetWindowLongA(IntPtr hWnd, int nIndex, int dwNewLong);

        [DllImport("user32.dll")]
        static extern int GetWindowLongA(IntPtr hWnd, int nIndex);

        private void InitializeComponent()
        {
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(SearchTextBox));
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Right;
            this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(16, 16);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            this.pictureBox1.TabIndex = 0;
            this.pictureBox1.TabStop = false;
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }
    }
}
